#include "ArgvKeeper.h"

#include <mutex>

namespace
{
    std::recursive_mutex     mutex;                  //!< mutex to protect access to static data
    std::vector<std::string> argv_list;              //!< cmd line arguments
    bool                     isInitialized = false;  //!< \c true if cmd line arguments were initialized
}  // namespace

namespace SG::Base
{
    //! \brief Store cmd line arguments
    void SetArgcArgv (int argc, char* argv[])
    {
        std::vector<std::string> tmp_list;
        for (int i = 0; i < argc; ++i)
            tmp_list.push_back (std::string (argv[i]));
        SetArgcArgv (tmp_list);
    }

    //! \overload void SetArgcArgv(int argc, char* argv[])
    void SetArgcArgv (const std::vector<std::string>& argv)
    {
        std::lock_guard<std::recursive_mutex> g (mutex);
        if (!isInitialized && !argv.empty ())
        {
            isInitialized = true;
            for (const std::string& value : argv)
            {
                argv_list.push_back (value);
            }
        }
    }

    //! \brief Get cmd line arguments
    std::vector<std::string> GetArgcArgv ()
    {
        std::lock_guard<std::recursive_mutex> g (mutex);
        return argv_list;
    }

    //! \brief Check if cmd line arguments were initialized
    //!
    //! In debug mode returns \c true if SetArgcArgv() was called or \c false otherwise.
    //! In release mode always returns \c true.
    bool ArgcArgvInitialized ()
    {
#ifdef DEBUG
        return isInitialized;
#else
        return true;
#endif
    }

}  // namespace Base